package com.songcha.library_common.util

import java.text.SimpleDateFormat
import java.util.*

class TimeUtil {
    companion object{
        @JvmStatic
        fun getTimeByMillSecond(ms:Int?):String{
            if(ms==null) return ""
            val s:Int=ms/1000
            var str=""
            if(s<60){
                str=String.format("00:%02d",s)
            }else{
                val minute=(s/60)
                val second=s%60
                str=String.format("%02d:%02d",minute,second)
            }

            return str

        }

        @JvmStatic
        fun getDayInWeek():Int{
            val calendar = Calendar.getInstance()
            val day = calendar.get(Calendar.DAY_OF_WEEK)
            return day
        }

        @JvmStatic
        fun getCurrentHour():Int{
            val calendar = Calendar.getInstance()
            val hour = calendar.get(Calendar.HOUR_OF_DAY)
            return hour
        }

        @JvmStatic
        fun formatTimeFullString(timeStamp:Long):String{
            val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
            return sdf.format(Date(timeStamp))
        }

        @JvmStatic
        fun formatTimeHHmm(timeStamp:Long):String{
            val sdf = SimpleDateFormat("HH:mm")
            return sdf.format(Date(timeStamp))
        }

        @JvmStatic
        fun formatTimeYYYYMMDD(timeStamp:Long):String{
            val sdf = SimpleDateFormat("yyyy-MM-dd")
            return sdf.format(Date(timeStamp))
        }

        /***
         * get today zero timestamp
         */
        @JvmStatic
        fun getTodayZeroTimeStamp():Long{
            //Greenwich time start with 1970-1-1:0:0，chiness time start with 1970-1-1:8:0
            val curTime = getCurrentTimeSecond()
            val todayStartTime = ((curTime + 28800) / 86400).toInt() * 86400 - 28800
            return todayStartTime.toLong()
        }

        @JvmStatic
        fun getCurrentTime():Long{
            return System.currentTimeMillis()
        }

        /***
         * get beijin time
         */
        @JvmStatic
        fun getCurrentTimeSecond():Long{
            return System.currentTimeMillis() / 1000
        }

        @JvmStatic
        fun timeString2Stamp(dateStr:String):Long{
            if(dateStr.isNullOrBlank()) return 0
            val dateFormat =  SimpleDateFormat("yyyy-MM-dd HH:mm")
            try {
                val date = dateFormat.parse(dateStr)
                return date.time
            } catch (e:Exception) {
                e.printStackTrace()
            }
            return 0
        }
    }
}